#!/usr/bin/env bash
################################################################################
# Test objective: check if a zero-copy monitor is correctly blocked until the
#                 monitored persistent VALE port reads the frame.
# Operations:
# 1) create a persistent VALE port (v0).
# 2) connect v0 and a VALE ephimeral port (v1) to the same VALE switch (vale0).
# 3) open a zero-copy monitor v0/z for v0.
# 4) send from v1 without receiving from v0, check that v0/z doesn't receive the
#    rame.
# 5) receive from v0, check that v0/z receives the frame.
################################################################################
source test_lib

parse_send_recv_arguments "$@"
verbosity="${verbosity:-}"
fill="${fill:-d}"
len="${len:-150}"
num="${num:-1}"
seq="${seq:-}"

create_vale_persistent_port "v0"
attach_to_vale_bridge "vale0" "v0"
# Pre-opening interface that will be needed. This is needed to avoid a race
# condition between the sending and receiving ports.
$FUNCTIONAL $verbosity -i "vale0:v0"
check_success $? "pre-open vale0:v0"
$FUNCTIONAL $verbosity -i "netmap:v0/z"
check_success $? "pre-open netmap:v0/z"
$FUNCTIONAL $verbosity -i "vale0:v1"
check_success $? "pre-open vale0:v1"

# Initially we don't receive with the monitored VALE port v0, therefore the
# monitor should not receive the frame.
$FUNCTIONAL $verbosity -i netmap:v0/z -r "${len}:${fill}:${num}" $seq -n &
p1=$!
$FUNCTIONAL $verbosity -i vale0:v1    -t "${len}:${fill}:${num}" $seq
e2=$?
wait $p1
e1=$?
check_success $e1 "no-receive-${num} netmap:v0/z"
check_success $e2 "send-${num} vale0:v0"

# Now we receive with the monitored VALE port v0, therefore the monitor should
# receive the frame.
$FUNCTIONAL $verbosity -i "vale0:v0"    -r "${len}:${fill}:${num}" $seq &
p3=$!
$FUNCTIONAL $verbosity -i "netmap:v0/z" -r "${len}:${fill}:${num}" $seq
e4=$?
wait $p3
e3=$?
check_success $e3 "receive-${num} vale0:v0"
check_success $e4 "receive-${num} netmap:v0/z"
